package fragment.recommendfragments;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.AboutRecomEntity;
import entity.ListAnchorEntity;
import fragment.AbsBaseFragment;
import utils.adapter.RecomendaboutAdpter;
import volley.VolleySingleton;

/**
 * 相关推荐
 * Created by dllo on 15/12/5.
 */
public class Recomendaboutfragment extends AbsBaseFragment implements View.OnClickListener {
    private ListView listView;
    private TextView title;
    private ImageView back;
    private RecomendaboutAdpter adpter;
    private   List<AboutRecomEntity.AlbumsEntity> datas;

    private long aid;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_reabout;
    }

    public Recomendaboutfragment(){

    }

    public  Recomendaboutfragment(long aid){
        this.aid = aid;
        Log.d("Recomendaboutfragment", "aid:" + aid);
    }


    @Override
    protected void initView() {
        listView = findView(R.id.aboutlist);
        title = findView(R.id.titleForAllTv);
        back = findView(R.id.titleForAllBack);
        title.setText("专辑");
        adpter = new RecomendaboutAdpter();
        listView.setAdapter(adpter);
    }

    @Override
    protected void initData() {
        initNetWork();
        back.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                goToNext(new ReCommendDetailsFragment(datas.get(position).getAlbumId(), position));
            }
        });

    }


 private void initNetWork(){
    String URL = "http://ar.ximalaya.com/rec-association/recommend/album/by_album?" +
             "albumId=" +aid+
             "&device=android";
     Log.d("Recomendaboutfragment", URL);
         VolleySingleton.getInstance().addRequest(URL,
                 AboutRecomEntity.class, new Response.Listener<AboutRecomEntity>() {
                     @Override
                     public void onResponse(AboutRecomEntity response) {

                         if (response != null) {
                             datas = response.getAlbums();
                             if (datas != null) {
                                 adpter.addData(datas);
                             }
                             dismissDialog();
                         }
                     }
                 }, new Response.ErrorListener() {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         dismissDialog();
                         toastError();
                     }
                 });
         showDialog();

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.titleForAllBack:
                goBack();
                break;
        }
    }


}
